// components/computed-info/index.js
import {
  chemicalAlias
} from '../../dataSource'
let t;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
      observer(data) {
        this.setFormat(data)
      }
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    activeName: '0',
    chemicalShow: false,
    dataSource: [],
    meltingTemperature: '',
    kz: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**处理小数点 */
    format(data) {
      if (data) {
        return parseFloat(data).toFixed(2);
      }
      return 0
    },
    setFormat(data = {}) {
        clearTimeout(t);
        t = setTimeout(()=>{
          this.setData({
            kz: this.format(data?.kz),
            meltingTemperature: this.format(data?.meltingTemperature)
          })
        }, 1000)
    },
    onChange(event) {
      this.setData({
        activeName: event.detail,
      });
    },
    chemicalCloseHandle() {
      this.setData({
        chemicalShow: false
      })
    },
    chemicalOpenHandle(event) {
      const key = event.currentTarget.dataset.value;
      this.setDataSource(key);
      this.setData({
        chemicalShow: true
      })
    },

    /**
     * 设置自动填充的表单
     * @param {*} param0 
     */
    setDataSource(key) {
      if (Object.keys(this.properties.data).length < 1) {
        wx.showToast({
          title: '暂无数据',
          icon: 'error'
        })
        return
      }
      let data = [],
        defaultValue = {};
      if (['elementCoefficientOfExpansionMap', 'firingTemperatureMap'].includes(key)) {
        data = Object.keys(this.properties.data.mooreCoefficientOfExpansionResult[key] || {});
        defaultValue = this.properties.data.mooreCoefficientOfExpansionResult[key]
      } else if (key === 'meltingTemperatureMap') {
        data = Object.keys(this.properties.data.mooreCoefficientOfExpansionResult.meltingTemperatureResult[key] || {});
        defaultValue = this.properties.data.mooreCoefficientOfExpansionResult.meltingTemperatureResult[key]
      } else {
        data = Object.keys(this.properties.data[key] || {});
        defaultValue = this.properties.data[key]
      }
      const array = data.map(it => {
        return {
          text: it === 'correctionAmount' ? '修正量' : chemicalAlias[it],
          value: it,
          defaultValue: this.format(defaultValue[it]?.substring(0, 10))
        }
      })
      this.setData({
        dataSource: array
      })
    }
  }
})